﻿using System.Data;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using YiTu.DBUtility;
using TOUR.SZHOME.Entity;
using tour.szhome.map.DTO;

namespace TOUR.SZHOME.DAL
{
    public partial class Projects_UnitDAL
    {
        /// <summary>
        /// 获取最新的一条主推户型
        /// </summary>
        /// <returns></returns>
        public static Projects_UnitEntity GetZTHXModel(int pid)
        {
            Projects_UnitEntity entity = new Projects_UnitEntity();
            string sql = "select top(1) * from Projects_Unit where XMID = "+ pid +" and SFZTHX = 1 order by HXID desc";
            using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, sql, null))
            {
                if (reader.Read())
                {
                    entity = ConvertToEntityFromDataReader(reader);
                }
            }
            return entity;
        }

        /// <summary>
        /// 根据条件获取户型列表 -- 异步
        /// </summary>
        /// <returns></returns>
        public static List<UnitDTO> GetHXListByAttr(int pid, int type)
        {
            List<UnitDTO> list = new List<UnitDTO>();
            string sql = string.Empty;
            if (type == 1)
            {
                sql = @"select HXID,XMID,XMLX,COUNT(0) as Total from Projects_Unit
                        where XMID = @XMID  
                        group by HXID,XMID,XMLX";
            }
            else
            {
                sql = @"select HXID,XMID,XMHX,COUNT(0) as Total from Projects_Unit 
                        where XMID = @XMID 
                        group by HXID,XMID,XMHX";
            }
            List<SqlParameter> commandParms = new List<SqlParameter>();
            commandParms.Add(SqlHelper.CreateParam("@XMID", SqlDbType.Int, 0, ParameterDirection.Input, pid));
            using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, sql, commandParms))
            {
                while (reader.Read())
                {
                    list.Add(new UnitDTO() {
                        XMID = Convert.ToInt32(reader["XMID"]),
                        HXID = Convert.ToInt32(reader["HXID"]),
                        ATTR = (type == 1 ? reader["XMLX"].ToString() : reader["XMHX"].ToString()),
                        Total = Convert.ToInt32(reader["Total"])
                    });
                }
            }
            return list;
        }

        /// <summary>
        /// 获取项目 -- 户型 -- 某种Type -- 展开详情（带图） -- 暂不分页
        /// </summary>
        /// <param name="pid">项目ID</param>
        /// <param name="attr">属性： 跟据Type不同入参也不同</param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<Projects_UnitEntity> GetHXDetailByAttr(int pid, string attr, int type)
        {
            List<Projects_UnitEntity> list = new List<Projects_UnitEntity>();
            string sql = string.Empty;
            if (type == 1)
            {
                sql = @"select * from Projects_Unit
                        where XMID = @XMID and XMLX = @XMLX ";
            }
            else
            {
                sql = @"select * from Projects_Unit 
                        where XMID = @XMID and XMHX = @XMHX";
            }
            List<SqlParameter> commandParms = new List<SqlParameter>();
            commandParms.Add(SqlHelper.CreateParam("@XMID", SqlDbType.Int, 0, ParameterDirection.Input, pid));
            commandParms.Add(SqlHelper.CreateParam("@XMLX", SqlDbType.NVarChar, 0, ParameterDirection.Input, attr));
            commandParms.Add(SqlHelper.CreateParam("@XMHX", SqlDbType.NVarChar, 0, ParameterDirection.Input, attr));

            using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, sql, commandParms))
            {
                while (reader.Read())
                {
                    list.Add(ConvertToEntityFromDataReader(reader));
                }
            }
            return list;
        }
    }
}
